package com.yoo.module_base.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.net.ConnectivityManager
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import com.yoo.module_base.model.ScanWifiModel

object SystemInfoUtil {
    private fun getWifi(activity: Activity): String {
        if (activity.isDestroyed){
            return ""
        }
        val wifiManager: WifiManager = activity.getSystemService(
            Context.WIFI_SERVICE
        ) as WifiManager
        val wifiInfo: WifiInfo = wifiManager.connectionInfo
        return wifiInfo.bssid
    }
    @SuppressLint("MissingPermission")
    fun getWifiList(activity: Activity): List<ScanWifiModel> {
            val wifiManager =
                activity.getSystemService(Context.WIFI_SERVICE) as WifiManager
            val scanWifiList = wifiManager.scanResults
            val wifiList: MutableList<ScanWifiModel> = ArrayList()
            val myBssid = getWifi(activity)
            if (scanWifiList != null && scanWifiList.size > 0) {
                for (i in scanWifiList.indices) {
                    val scanResult = scanWifiList[i]
                    if (scanResult.SSID.isNotEmpty()) {
                        val scanWifiBean = ScanWifiModel(
                            scanResult.SSID,
                            scanResult.BSSID,
                            if (myBssid == scanResult.BSSID) 0 else 1
                        )
                        wifiList.add(scanWifiBean)
                    }
                }
            }
            return wifiList
    }

    fun isWifiEnabled(context: Context?): Boolean {
        if (context == null) {
            throw NullPointerException("Global context is null")
        }
        val wifiMgr = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
        return if (wifiMgr.wifiState == WifiManager.WIFI_STATE_ENABLED) {
            val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            wifiInfo!!.isConnected
        } else {
            false
        }
    }


}